package spu

import (
	"context"
	"errors"
	"github.com/google/uuid"
	"github.com/kuangshp/go-utils/k"
	"github.com/shopspring/decimal"
	"github.com/spf13/cast"
	"insighttracking/apps/admin/internal/logic/category"
	"insighttracking/query/dao"
	"net/http"
	"strings"

	"insighttracking/apps/admin/internal/svc"
	"insighttracking/apps/admin/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
	"os"
)

type ModifySpuByIdApiLogic struct {
	logx.Logger
	ctx     context.Context
	svcCtx  *svc.ServiceContext
	Request *http.Request
}

func NewModifySpuByIdApiLogic(ctx context.Context, svcCtx *svc.ServiceContext, request *http.Request) *ModifySpuByIdApiLogic {
	hostname, _ := os.Hostname()
	return &ModifySpuByIdApiLogic{
		Logger: logx.WithContext(ctx).WithCallerSkip(0).WithFields(
			logx.Field("Log_UUID", uuid.New().String()),
			logx.Field("hostname", hostname)),
		ctx:     ctx,
		svcCtx:  svcCtx,
		Request: request,
	}
}

func (l *ModifySpuByIdApiLogic) ModifySpuByIdApi(req *types.ModifySpuReq) (resp string, err error) {
	salesPrice, _ := decimal.NewFromString(req.SalesPrice)
	lossRate, _ := decimal.NewFromString(req.LossRate)
	purchasePrice, _ := decimal.NewFromString(req.PurchasePrice)
	categoryIdList := k.Map(strings.Split(req.CategoryIDList, ","), func(item string, index int) int64 {
		return cast.ToInt64(item)
	})
	categoryLogic := category.NewGetCategoryListApiLogic(l.ctx, l.svcCtx, l.Request)
	categoryName, _ := categoryLogic.GetCategoryNameApi(categoryIdList)
	if _, err = dao.SpuEntity.WithContext(l.ctx).Where(dao.SpuEntity.ID.Eq(req.Id)).UpdateSimple(
		dao.SpuEntity.Name.Value(req.Name),                                    // 商品名称
		dao.SpuEntity.CategoryID.Value(categoryIdList[len(categoryIdList)-1]), // 商品分类,只保存最后一级的
		dao.SpuEntity.CategoryName.Value(categoryName),                        // 单位分类名称
		dao.SpuEntity.CategoryIDList.Value(req.CategoryIDList),                // 分类列表,多个用英文,拼接
		dao.SpuEntity.UnitID.Value(req.UnitID),                                // 单位,关联到unit表主键id
		dao.SpuEntity.IsWeighed.Value(req.IsWeighed),                          // 是否称重,0表示是,1表示否
		dao.SpuEntity.SalesPrice.Value(salesPrice),                            // 销售价
		dao.SpuEntity.Status.Value(req.Status),                                // 上架状态:1是上架,2是下架
		dao.SpuEntity.Sort.Value(req.Sort),                                    // 排序
		dao.SpuEntity.ProductImageURL.Value(req.ProductImageURL),              // 商品图
		dao.SpuEntity.SupplierID.Value(req.SupplierID),                        // 供应商id
		dao.SpuEntity.BrandID.Value(req.BrandID),                              // 品牌id
		dao.SpuEntity.LossRate.Value(lossRate),                                // 损耗率
		dao.SpuEntity.PurchasePrice.Value(purchasePrice),                      // 采购价格
		dao.SpuEntity.Content.Value(req.Content),                              // 商品详情
		dao.SpuEntity.Spec.Value(req.Spec),                                    // 规格
		dao.SpuEntity.Period.Value(req.Period),                                // 保质期
		dao.SpuEntity.IsSelfPurchase.Value(req.IsSelfPurchase),                // 是否自备采购
	); err != nil {
		return "", errors.New("创建失败")
	}
	return "修改成功", nil
}
